package com.zuowei.ycy.user.ui.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.and.yzy.frame.util.RetrofitUtils;
import com.zuowei.ycy.R;
import com.zuowei.ycy.user.config.UserManger;
import com.zuowei.ycy.user.domain.AddressInfo;
import com.zuowei.ycy.user.http.Address;
import com.zuowei.ycy.ui.BaseToolbarAty;
import com.zuowei.ycy.user.util.AppJsonUtil;

import butterknife.Bind;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/4/27.
 * 添加收货地址
 */
public class AddaddressAty extends BaseToolbarAty {

    @Bind(R.id.edit_name)
    EditText editName;
    @Bind(R.id.edit_phone)
    EditText editPhone;
    @Bind(R.id.edit_address)
    EditText editAddress;
    @Bind(R.id.cbox_defult)
    CheckBox cboxDefult;
    @Bind(R.id.fbtn_save)
    FButton fbtnSave;
    private String type = "";
    private AddressInfo info;

    @Override
    public int getLayoutId() {
        return R.layout.add_address_aty;
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getString("type");
            info = (AddressInfo) bundle.getSerializable("info");
            initViewData();
        }
        if (type.equals("1")) {
            mToolbar.setTitle("编辑收货地址");
        } else {
            mToolbar.setTitle("添加收货地址");
        }


    }

    private void initViewData() {
        editName.setText(info.getConsignee());
        editPhone.setText(info.getMobile());
        editAddress.setText(info.getAddress());
        if (info.getIs_default().equals("1")) {
            cboxDefult.setChecked(true);
        }else {
            cboxDefult.setChecked(false);
        }
    }

    @Override
    public void requestData() {

    }

    @OnClick({R.id.fbtn_save})
    @Override
    public void btnClick(View view) {
        super.btnClick(view);
        switch (view.getId()) {
            case R.id.fbtn_save:
                String name = editName.getText().toString();
                String mobile = editPhone.getText().toString();
                String address = editAddress.getText().toString();
                String is_default = cboxDefult.isChecked()?"1":"0";

                if (type.equals("1")){ //编辑
                    showLoadingDialog("");
                    doHttp(RetrofitUtils.createApi(Address.class).modifyAddress(info.getAddress_id(), UserManger.getM_id(),name,mobile,address,is_default),1);
                }else { //增加
                    showLoadingDialog("");
                    doHttp(RetrofitUtils.createApi(Address.class).addAddress(UserManger.getM_id(),name,mobile,address,is_default),1);
                }

                break;

        }
    }

    @Override
    public void onSuccess(String result, Call<ResponseBody> call, Response<ResponseBody> response, int what) {
        if (what==0||what==1){
            showToast(AppJsonUtil.getResultInfo(result).getMessage());
            finish();
        }
        super.onSuccess(result, call, response, what);
    }
}
